import { Options, PipelineOperator } from "../../core";
import { Iterator } from "../../lazy";
import {AsyncIterator} from "../../async/lazy";
/**
 * Restricts the number of documents in an aggregation pipeline.
 *
 * See {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit/ usage}.
 *
 * @param collection
 * @param value
 * @param options
 * @returns
 */
export const $limit: PipelineOperator = (
  collection: Iterator|AsyncIterator,
  expr: number,
  _options: Options
): Iterator|Promise<AsyncIterator> => {
  if(_options.isAsync) {
    return Promise.resolve(collection as AsyncIterator).then((collection) => {
      return collection.take(expr);
    });
  }
  return (collection as Iterator).take(expr)
};
